package com.game.business.controller;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.io.IOException;

import com.game.business.domain.TNotice;
import com.game.business.domain.TWallet;
import com.game.business.service.ITNoticeService;
import com.game.business.service.ITWalletService;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.core.domain.entity.SysUser;
import com.game.common.enums.BusinessType;
import com.game.common.utils.DateUtils;
import com.game.common.utils.SecurityUtils;
import com.game.common.utils.StringUtils;
import com.game.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;

import com.game.system.service.ISysUserService;
import com.game.system.service.impl.SysUserServiceImpl;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.game.business.domain.TUserSfsq;
import com.game.business.service.ITUserSfsqService;
import com.game.common.core.page.TableDataInfo;

/**
 * 身份审核Controller
 *
 * @author ruoyi
 * @date 2025-10-11
 */
@RestController
@RequestMapping("/business/sfsq")
public class TUserSfsqController extends BaseController {
    @Autowired
    private ITUserSfsqService tUserSfsqService;


/**
 * 查询身份审核列表
 */
//@RequiresPermissions("business:sfsq:list")
@GetMapping("/list")
    public TableDataInfo list(TUserSfsq tUserSfsq) {
        startPage();
        List<TUserSfsq> list = tUserSfsqService.selectTUserSfsqList(tUserSfsq);
        return getDataTable(list);
    }

    /**
     * 导出身份审核列表
     */
    //@RequiresPermissions("business:sfsq:export")
    @Log(title = "身份审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUserSfsq tUserSfsq) {
        List<TUserSfsq> list = tUserSfsqService.selectTUserSfsqList(tUserSfsq);
        ExcelUtil<TUserSfsq> util = new ExcelUtil<TUserSfsq>(TUserSfsq. class);
        util.exportExcel(response, list, "身份审核数据");
    }

    /**
     * 获取身份审核详细信息
     */
    //@RequiresPermissions("business:sfsq:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tUserSfsqService.selectTUserSfsqById(id));
    }

    /**
     * 新增身份审核
     */
    //@RequiresPermissions("business:sfsq:add")
    @Log(title = "身份审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUserSfsq tUserSfsq) {
        return toAjax(tUserSfsqService.save(tUserSfsq));
    }

    /**
     * 修改身份审核
     */
    //@RequiresPermissions("business:sfsq:edit")
    @Log(title = "身份审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUserSfsq tUserSfsq) {
        return toAjax(tUserSfsqService.updateById(tUserSfsq));
    }

    /**
     * 删除身份审核
     */
    //@RequiresPermissions("business:sfsq:remove")
    @Log(title = "身份审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tUserSfsqService.removeByIds(Arrays.asList(ids)));
    }

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    private ITNoticeService tNoticeService;

    @Autowired
    private ITWalletService tWalletService;
    /**
     * @Author:zhaoyongke
     * @Description:
     */
    @PostMapping("checkXx")
    @Transactional
    public AjaxResult checkXx(@RequestBody TUserSfsq vo)
    {
        String checkStatus = vo.getCheckStatus();
        String checkRemark = vo.getCheckRemark();
        String id = vo.getId();
        if(StringUtils.isEmpty(id)){
            return  AjaxResult.error("参数获取失败");
        }
        if("3".equals(checkStatus)  && StringUtils.isEmpty(checkRemark)){
            return  AjaxResult.error("请填写审核备注");
        }
        TUserSfsq sq = tUserSfsqService.getById(id);
        sq.setCheckStatus(checkStatus);
        sq.setCheckDate(DateUtils.getNowDate());
        sq.setCheckRemark(checkRemark);
        tUserSfsqService.updateById(sq);
        String type = sq.getType();

        TNotice notice = new TNotice();
        notice.setTilte("身份审核结果");
        if("2".equals(checkStatus)){
            SysUser sysUser =new SysUser();
            sysUser.setUserId(Long.valueOf(sq.getUserId()));
            //1 担保人 2 代练师
            if("1".equals(type)){
                sysUser.setIsDbr("1");
                notice.setContent("您的担保师身份审核通过了");
            }else{
                sysUser.setIsDls("1");
                notice.setContent("您的代练师身份审核通过了");
            }
            sysUserService.updateUserApp(sysUser);
        }else{
            if("1".equals(type)){
                // 用户余额返回
                TWallet tWallet = new TWallet();
                tWallet.setUserId(SecurityUtils.getUserIdStr());
                tWallet.setMoney(sq.getMoney());
                tWallet.setType("9");
                tWallet.setRemark("申请担保人驳回退钱");
                tWalletService.save(tWallet);
                notice.setContent("您的担保师身份审核驳回了");
            }else{
                notice.setContent("您的代练师身份审核驳回了");
            }
        }
        notice.setSendUserId(SecurityUtils.getUserIdStr());
        notice.setReceiveUserId(sq.getUserId());
        notice.setIsRead("2");
        tNoticeService.save(notice);
        return AjaxResult.success();
    }

}
